package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
import android.text.TextUtils;
/*Start by menglingqiang*/
import android.util.Log;
/*End by menglingqiang*/
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.adapter.PaidAlbumTracksAdapter;
/*Start by menglingqiang*/
import com.jpcc.vw.onlineRadio.application.MusicApplication;
/*End by menglingqiang*/
import com.jpcc.vw.onlineRadio.base.BaseActivity;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.AlbumBrowseBean;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.Track;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
/*Start by menglingqiang*/
import com.jpcc.vw.tracelog.TracelogInterface;
/*End by menglingqiang*/
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
/*Start by menglingqiang*/
import java.util.Date;
/*End by menglingqiang*/
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action0;

public class BoughtAlbumTrack extends BaseActivity {


    @BindView(R.id.swipe_refrsh_tracks)
    SmartRefreshLayout swipe_refrsh_tracks;
    @BindView(R.id.recyclerview_tracks)
    ListView recyclerview_tracks;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.layout_back)
    RelativeLayout layout_back;

    private IRadio radioModel;
    private long albumId;
    private boolean is_refresh = true;
    private int page_index = 1;
    private PaidAlbumTracksAdapter adapter;
    private List<Track> datas = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_bought_album_tracks;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        albumId = getIntent().getLongExtra("key_album_id", -1);
        radioModel = new RadioImpl();
        adapter = new PaidAlbumTracksAdapter(this, datas);
        recyclerview_tracks.setAdapter(adapter);
        recyclerview_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (TextUtils.isEmpty(datas.get(position).getPlayUrl32())) {
                    ToastUtils.show("play url is empty!");
                }
            }
        });
        swipe_refrsh_tracks.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refrsh_tracks.setEnableLoadmore(true);
                browsePaidAlbumTracks();
            }
        });
        swipe_refrsh_tracks.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                browsePaidAlbumTracks();
            }
        });
        is_refresh = true;
        browsePaidAlbumTracks();
    }


    private void browsePaidAlbumTracks() {
        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }

        /*Start by menglingqiang*/
        Date dt = new Date();
        try {
            TracelogInterface inter = MusicApplication.getLogInterface();
            if(inter != null){
                inter.sendLogData(dt.toString(),dt.toString(),BoughtAlbumTrack.this.getClass().getName(),"DEBUG",null,"BrowsePaidAlbumTracks request");
            }else {
                Log.e("jgj","inter == null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*End by menglingqiang*/

        radioModel.BrowsePaidAlbumTracks(albumId, 1).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new CommonSubscriber<AlbumBrowseBean>() {
            @Override
            public void onSuccess(AlbumBrowseBean response) {
                hideProgress();
                swipe_refrsh_tracks.finishRefresh();
                swipe_refrsh_tracks.finishLoadmore();
                if (response.getData() != null && response.getData().getTracks() != null) {
                    datas.clear();
                    datas.addAll(response.getData().getTracks());
                    adapter.setDatas(datas);
                }
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),BoughtAlbumTrack.this.getClass().getName(),"DEBUG",response.getStatusCode().toString(),"BrowsePaidAlbumTracks onSuccess");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }

            @Override
            public void onFailure(BaseBean response) {
                hideProgress();
                ToastUtils.show(response.getStatusMessage());
                swipe_refrsh_tracks.finishRefresh();
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),BoughtAlbumTrack.this.getClass().getName(),"ERROR",response.getStatusCode().toString(),"BrowsePaidAlbumTracks onFailure");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }
        });
    }

    @OnClick({R.id.layout_back, R.id.imv_top_search})
    void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.layout_back:
                BoughtAlbumTrack.this.finish();
                break;
            case R.id.imv_top_search:
                Intent intent = new Intent(BoughtAlbumTrack.this, SearchActivity.class);
                startActivity(intent);
                break;
        }
    }


}
